package com.lemon.funplus.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.funplus.AccountChangePassActivity;
import com.lemon.funplus.AccountLoginActivity;
import com.lemon.funplus.CateDetailsActivity;
import com.lemon.funplus.CheAnhActivity;
import com.lemon.funplus.MainActivity;
import com.lemon.funplus.NhacHaiActivity;
import com.lemon.funplus.R;
import com.lemon.funplus.UploadActivity;
import com.lemon.funplus.object.AccountObject;
import com.lemon.funplus.ulti.AppUtils;
import com.lemon.funplus.ulti.PreferenceUtils;
import com.lemon.funplus.ulti.TagName;
import com.lemon.funplus.view.MaterialSection;
import com.lemon.funplus.view.MaterialSectionListener;
import com.nineoldandroids.view.ViewHelper;

public class MenuFragment extends Fragment implements MaterialSectionListener {
	private LinearLayout menu_Header;
	private LinearLayout menu_Content;
	private LinearLayout menu_Bottom;

	private List<MaterialSection> headerMenuList;
	private List<MaterialSection> contentMenuList;
	private List<MaterialSection> bottomMenuList;
	public MaterialSection currentSection;
	private float density;
	private Resources resources;
	public static final int BOTTOM_SECTION_START = 100;

	public MaterialSection ACCOUNT;
	public MaterialSection HOME;
	public MaterialSection ANH_HAI;
	public MaterialSection CLIP_HAI;
	public MaterialSection PHIM_HAI;
	public MaterialSection NHAC_HAI;
	public MaterialSection TRUYEN_HAI;
	public MaterialSection UPLOAD;
	public MaterialSection CHE_ANH;
//	public MaterialSection DOWNLOAD;
	public MaterialSection CHIA_SE;
	public MaterialSection THONG_TIN;
	public MaterialSection CAI_DAT;
	public MaterialSection WAPFUN;
	public MaterialSection LOG_OUT;
	
	DrawerLayout drawer;

	private float offset;
	private boolean flipped;

	String strAvatar = "";
	String strName = "";
	
	AccountObject accountObject;
	
	public void setDrawerLayout(DrawerLayout drawer) {
		this.drawer = drawer;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.menu_layout, container, false);
		findViewById(view);
		strAvatar = PreferenceUtils.getString(getActivity(), TagName.user_avatar);
		strName = PreferenceUtils.getString(getActivity(), TagName.user_avatar);
		headerMenuList = new ArrayList<MaterialSection>();
		contentMenuList = new ArrayList<MaterialSection>();
		bottomMenuList = new ArrayList<MaterialSection>();
		resources = this.getResources();
		density = resources.getDisplayMetrics().density;

		createMenu();
		return view;
	}
	public void logoutDialog() {
		AlertDialog builder = new AlertDialog.Builder(getActivity()).create();
		builder.setMessage(getActivity().getResources().getString(R.string.logout_warning));
		builder.setCancelable(false);
		builder.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
//				changedLoginstatus(false);
				PreferenceUtils.remove(getActivity(), TagName.user_avatar);
				PreferenceUtils.remove(getActivity(), TagName.user_name);
				ACCOUNT.setTitle(resources.getString(R.string.menu_taikhoan));
				ACCOUNT.setIcon(resources.getDrawable(R.drawable.menu_user_icon));
				ACCOUNT.setTarget(new Intent(getActivity(), AccountLoginActivity.class));
				removeSection(LOG_OUT);
			}
		});
		builder.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
	
	public void toggleAccountstatus(){
		accountObject=AppUtils.getAccount(getActivity());
		strAvatar = accountObject.getavatar();
		strName = accountObject.getname();
//		Log.e("", "strAvatar: " +strAvatar);
//		Log.e("", "strName: " +strAvatar);
		if (strAvatar!=null || strName!=null){
			ACCOUNT.setTitle(strName);
			ACCOUNT.setIcon(strAvatar);
			ACCOUNT.setTarget(new Intent(getActivity(), AccountChangePassActivity.class));
			LOG_OUT=newSection("Đăng xuất", new MaterialSectionListener() {
				@Override
				public void onClick(MaterialSection section) {
						logoutDialog();
				}
			});
			addSection(LOG_OUT);
		}
	}
	
	void createMenu() {
//		if (strAvatar!=null || strName!=null)
//			ACCOUNT = newSection(strName, strAvatar,new Intent(getActivity(), AccountInfoActivity.class));
//		else
			ACCOUNT = newSection(resources.getString(R.string.menu_taikhoan), R.drawable.menu_user_icon, new Intent(getActivity(), AccountLoginActivity.class));
		HOME = newSection(resources.getString(R.string.menu_trangchu), R.drawable.menu_home, new Intent(getActivity(), MainActivity.class));
		ANH_HAI = newSection(resources.getString(R.string.menu_anhhai), R.drawable.menu_anhhai_icon, new Intent());
		CLIP_HAI = newSection(resources.getString(R.string.menu_cliphai), R.drawable.menu_video_icon, new Intent());
		PHIM_HAI = newSection(resources.getString(R.string.menu_phimhai), R.drawable.menu_film_icon, new Intent());
		NHAC_HAI = newSection(resources.getString(R.string.menu_nhachai), R.drawable.menu_nhac_icon, new Intent());
		TRUYEN_HAI = newSection(resources.getString(R.string.menu_truyenhai), R.drawable.menu_sachhai_icon, new Intent());
		//
		UPLOAD = newSection(resources.getString(R.string.menu_upload), R.drawable.menu_upload_icon, new Intent(getActivity(), UploadActivity.class));
		CHE_ANH = newSection(resources.getString(R.string.menu_cheanh), R.drawable.menu_cheanh_icon, new Intent(getActivity(), CheAnhActivity.class));
//		DOWNLOAD = newSection(resources.getString(R.string.menu_download), R.drawable.menu_download_icon, new Intent(getActivity(), MainActivity.class));
		CHIA_SE = newSection(resources.getString(R.string.menu_chiase), R.drawable.menu_share, new Intent(getActivity(), MainActivity.class));
		THONG_TIN = newSection(resources.getString(R.string.menu_thongtin), R.drawable.menu_info, new Intent(getActivity(), MainActivity.class));
		CAI_DAT = newSection(resources.getString(R.string.menu_caidat), R.drawable.menu_setup, new Intent(getActivity(), MainActivity.class));
		WAPFUN = newSection(resources.getString(R.string.menu_wapfun), R.drawable.menu_wap, new Intent(getActivity(), MainActivity.class));
		addSection(ACCOUNT);
		addSubheader(resources.getString(R.string.menu_danhmuc), menu_Content);
		ANH_HAI.setTarget(switchCateMenu("2", ANH_HAI.getTitle(), ANH_HAI.getTitle()));
		CLIP_HAI.setTarget(switchCateMenu("3", CLIP_HAI.getTitle(), CLIP_HAI.getTitle()));
		PHIM_HAI.setTarget(switchCateMenu("4", PHIM_HAI.getTitle(), PHIM_HAI.getTitle()));
		NHAC_HAI.setTarget(switchCateMenu("5", NHAC_HAI.getTitle(), NhacHaiActivity.class));
		TRUYEN_HAI.setTarget(switchCateMenu("6", TRUYEN_HAI.getTitle(), TRUYEN_HAI.getTitle()));
		
		addSection(HOME);
		addSection(ANH_HAI);
		addSection(CLIP_HAI);
		addSection(PHIM_HAI);
		addSection(NHAC_HAI);
		addSection(TRUYEN_HAI);
		
		addSubheader(resources.getString(R.string.menu_tools), menu_Content);
		addSection(UPLOAD);
		addSection(CHE_ANH);
//		addSection(DOWNLOAD);
		addSection(CHIA_SE);
		addSection(THONG_TIN);
		addSection(CAI_DAT);
		addSection(WAPFUN);
		addSubheader(resources.getString(R.string.menu_copyright), menu_Content);
		
//		if (strAvatar!=null || strName!=null){
//			LOG_OUT=newSection("Đăng xuất", new MaterialSectionListener() {
//				@Override
//				public void onClick(MaterialSection section) {
//						logoutDialog();
//				}
//			});
//			addSection(LOG_OUT);
//		}
		toggleAccountstatus();
	}
	
	public MaterialSection getSection(String section){
		if(section.equals(ANH_HAI.getTitle()))
			return ANH_HAI;
		if(section.equals(CLIP_HAI.getTitle()))
			return CLIP_HAI;
		if(section.equals(PHIM_HAI.getTitle()))
			return PHIM_HAI;
		if(section.equals(NHAC_HAI.getTitle()))
			return NHAC_HAI;
		if(section.equals(TRUYEN_HAI.getTitle()))
			return TRUYEN_HAI;
		if(section.equals(UPLOAD.getTitle()))
			return UPLOAD;
		if(section.equals(CHE_ANH.getTitle()))
			return CHE_ANH;
//		if(section.equals(DOWNLOAD.getTitle()))
//			return DOWNLOAD;
		if(section.equals(CHIA_SE.getTitle()))
			return CHIA_SE;
		if(section.equals(THONG_TIN.getTitle()))
			return THONG_TIN;
		if(section.equals(CAI_DAT.getTitle()))
			return CAI_DAT;
		if(section.equals(WAPFUN.getTitle()))
			return WAPFUN;
		return HOME;
	}
	
	Intent switchCateMenu(String cateid,String title,String section){
		Intent intent=new Intent(getActivity(), CateDetailsActivity.class);
		intent.putExtra(CateDetailsActivity.CATE_ID, cateid);
		intent.putExtra(CateDetailsActivity.CATE_TITLE, title);
		intent.putExtra(CateDetailsActivity.CATE_SECTION, section);
		return intent;
	}

	Intent switchCateMenu(String cateid,String title,Class<?> activity){
		Intent intent=new Intent(getActivity(), activity);
		intent.putExtra(CateDetailsActivity.CATE_ID, cateid);
		intent.putExtra(CateDetailsActivity.CATE_TITLE, title);
		intent.putExtra(CateDetailsActivity.CATE_SECTION, title);
		return intent;
	}
	
	void findViewById(View view) {
		menu_Header = (LinearLayout) view.findViewById(R.id.menu_header);
		menu_Content = (LinearLayout) view.findViewById(R.id.menu_content);
		menu_Bottom = (LinearLayout) view.findViewById(R.id.menu_bottom);
	}

	@Override
	public void onClick(final MaterialSection section) {
		if(currentSection!=null){
			if (!currentSection.getTitle().equals(section.getTitle())) {
				switch (section.getTarget()) {
				case MaterialSection.TARGET_FRAGMENT:
					// switchMenuFragment(section.getTargetFragment().getClass().getName());
					break;
				case MaterialSection.TARGET_ACTIVITY:
					drawer.closeDrawers();
					startActivity(section.getTargetIntent());
					// getActivity().finish();
					break;
				case MaterialSection.TARGET_LISTENER:
				default:
					break;
				}
				currentSection = section;
	
				int position = section.getPosition();
	
				SelectMenu(position);
			}
			else {
				section.select();
				drawer.closeDrawers();
			}
		}else{
			switch (section.getTarget()) {
			case MaterialSection.TARGET_FRAGMENT:
				// switchMenuFragment(section.getTargetFragment().getClass().getName());
				break;
			case MaterialSection.TARGET_ACTIVITY:
				drawer.closeDrawers();
				startActivity(section.getTargetIntent());
				// getActivity().finish();
				break;
			case MaterialSection.TARGET_LISTENER:
			default:
				break;
			}
			currentSection = section;

			int position = section.getPosition();

			SelectMenu(position);
		}
			

	}

	public void SelectMenu(int position) {
		for (MaterialSection mySection : headerMenuList) {
			if (position != mySection.getPosition())
				mySection.unSelect();
		}
		for (MaterialSection mySection : contentMenuList) {
			if (position != mySection.getPosition())
				mySection.unSelect();
		}
		for (MaterialSection mySection : bottomMenuList) {
			if (position != mySection.getPosition())
				mySection.unSelect();
		}
	}

	public void addHeaderSection(MaterialSection section) {
		section.setPosition(contentMenuList.size());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (48 * density));
		headerMenuList.add(section);
		menu_Header.addView(section.getView(), params);
	}

	public void addSection(MaterialSection section) {
		section.setPosition(contentMenuList.size());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (48 * density));
		contentMenuList.add(section);
		menu_Content.addView(section.getView(), params);
	}

	public void removeSection(MaterialSection section){
		contentMenuList.remove(section);
		menu_Content.removeView(section.getView());
	}
	
	public void addBottomSection(MaterialSection section) {
		section.setPosition(BOTTOM_SECTION_START + bottomMenuList.size());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (48 * density));
		bottomMenuList.add(section);
		menu_Bottom.addView(section.getView(), params);
	}

	public void addDivisor(ViewGroup viewGroup) {
		View view = new View(getActivity());
		view.setBackgroundColor(Color.parseColor("#8C8F8D"));
		// height 1 px
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
		params.setMargins(10, (int) (8 * density), 10, (int) (8 * density));

		viewGroup.addView(view, params);
	}

	public void addSubheader(CharSequence title, ViewGroup viewGroup) {
		View subheader = LayoutInflater.from(getActivity()).inflate(R.layout.layout_material_subheader, viewGroup, false);
		TextView subheaderTitle = (TextView) subheader.findViewById(R.id.subheader_text);
		subheaderTitle.setText(title);

		// add custom separator on top of subheader
		View view = new View(getActivity());
		view.setBackgroundColor(Color.parseColor("#ffffff"));
		// view.setAlpha(0.54f);
		ViewHelper.setAlpha(view, 0.54f);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
		params.setMargins(10, (int) (8 * density), 10, 0);

		viewGroup.addView(subheader);
		// viewGroup.addView(view, params);
	}

	public MaterialSection newSection(String title, Drawable icon, Fragment target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_FRAGMENT);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, String icon, Fragment target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_FRAGMENT);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Drawable icon, Intent target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_ACTIVITY);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}
	
	public MaterialSection newSection(String title, String icon, Intent target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_ACTIVITY);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Drawable icon, MaterialSectionListener target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_LISTENER);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Bitmap icon, Fragment target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_FRAGMENT);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Bitmap icon, Intent target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_ACTIVITY);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Bitmap icon, MaterialSectionListener target) {
		MaterialSection section = new MaterialSection(getActivity(), true, MaterialSection.TARGET_LISTENER);
		section.setOnClickListener(this);
		section.setIcon(icon);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, int icon, Fragment target) {
		return newSection(title, resources.getDrawable(icon), target);
	}

	public MaterialSection newSection(String title, int icon, Intent target) {
		return newSection(title, resources.getDrawable(icon), target);
	}

	public MaterialSection newSection(String title, int icon, MaterialSectionListener target) {
		return newSection(title, resources.getDrawable(icon), target);
	}

	public MaterialSection newSection(String title, Fragment target) {
		MaterialSection section = new MaterialSection(getActivity(), false, MaterialSection.TARGET_FRAGMENT);
		section.setOnClickListener(this);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, Intent target) {
		MaterialSection section = new MaterialSection(getActivity(), false, MaterialSection.TARGET_ACTIVITY);
		section.setOnClickListener(this);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection newSection(String title, MaterialSectionListener target) {
		MaterialSection section = new MaterialSection(getActivity(), false, MaterialSection.TARGET_LISTENER);
		section.setOnClickListener(this);
		section.setTitle(title);
		section.setTarget(target);

		return section;
	}

	public MaterialSection getCurrentSection() {
		return currentSection;
	}
}
